﻿#region Using directives

using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Weazel.Toad.Application.Items;

#endregion

namespace Weazel.Toad.Application.Controls
{
  [ToolboxItem(true)]
  public partial class ConsoleOutputControl : UserControl
  {
    private EventHandler<SimulationOutputReadyEventArgs> outputReadyEventHandler;
    private void handleOutputReadyEvent(object sender, SimulationOutputReadyEventArgs e)
    {
      if(e.Text != "")
        consoleOutputTextBox1.AppendText(e.Text);			  
    }

    private EventHandler<SimulationEventArgs> simulationStartedEventHandler;
    private void HandleSimulationStartedEvent(object sender, SimulationEventArgs args)
    {
      // clear 
			consoleOutputTextBox1.Clear();
    }

    public ConsoleOutputControl()
    {
      InitializeComponent();

      outputReadyEventHandler =
        new EventHandler<SimulationOutputReadyEventArgs>(handleOutputReadyEvent);

      simulationStartedEventHandler =
        new EventHandler<SimulationEventArgs>(HandleSimulationStartedEvent);

      Application.Instance.SelectedItemChanging += 
        new EventHandler<SelectedItemChangedEventArgs>(Instance_SelectedItemChanging);

      Application.Instance.SelectedItemChanged += 
        new EventHandler<SelectedItemChangedEventArgs>(Instance_SelectedItemChanged);
    }

    public void Clear()
    {
			consoleOutputTextBox1.Clear();
    }

    public void Append(char c)
    {
      consoleOutputTextBox1.Append(c);
    }

    void Instance_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
    {
      if (e.SelectedItem is Items.GezelTextFile)
      {      
        Items.GezelTextFile gezelFile = 
          e.SelectedItem as Items.GezelTextFile;

        // set new content
				consoleOutputTextBox1.Clear();
				consoleOutputTextBox1.AppendText(gezelFile.Simulation.OutputBuffer.ToString());

        // register handler 
        gezelFile.Simulation.SimulationOutputReady += handleOutputReadyEvent;
        gezelFile.Simulation.SimulationStarted += simulationStartedEventHandler;

        // also hookup to the co-simulation code
        //Application.Instance.MicroblazeCosimulation.SimulationOutputReady += handleOutputReadyEvent;
        //Application.Instance.MicroblazeCosimulation.SimulationStarted += simulationStartedEventHandler;


        Enabled = true;
      }
      else
      {
        Enabled = false;
      }
    }

    void Instance_SelectedItemChanging(object sender, SelectedItemChangedEventArgs e)
    {
      if (e.SelectedItem is Items.GezelTextFile)
      {
        Items.GezelTextFile gezelFile =
          e.SelectedItem as Items.GezelTextFile;

        gezelFile.Simulation.SimulationOutputReady -= handleOutputReadyEvent;
        gezelFile.Simulation.SimulationStarted -= simulationStartedEventHandler;
      }
      else
      {
       
      }
		}
  }
}
